package com.zhangwei.moni_two.controller;

import java.text.NumberFormat;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhangwei.moni_two.entity.User;
import com.zhangwei.moni_two.service.UserService;


@Controller
public class UserController {
	
	@Autowired
	private UserService service;
	
	@RequestMapping("toLogin")
	public String toLogin() {
		
		return "login";

	}
	
	
	@RequestMapping("login")
	@ResponseBody
	public Integer login(User user,HttpSession session) {
		// TODO Auto-generated method stub
		//判断登陆名是否存在
		Integer integer = service.selLoginName(user,session);
		
		return integer;
	}
	
	@RequestMapping("list")
	public String list(Model model,@RequestParam(defaultValue="1")Integer pageNum,
			@RequestParam(defaultValue="2")Integer pageSize) {
		
		PageInfo<User> info = service.selAllUser(pageNum,pageSize);
		model.addAttribute("info", info);
		Integer count = service.selAllCount();
		Integer integer = service.selByInformation(1);
		Integer integer2 = service.selByInformation(0);
		model.addAttribute("count", count);
		model.addAttribute("integer", integer);
		model.addAttribute("integer2", integer2);

		NumberFormat instance = NumberFormat.getInstance();
		instance.setMaximumFractionDigits(2);
		String format = instance.format((float)integer/(float)count*100);
		String format2 =instance.format((float)integer2/(float)count*100);		
		
		model.addAttribute("bfb", format);
		model.addAttribute("bfb2", format2);
		
		
		return "list";

	}
	
	
	
	
	

}
